package com.xx.sg.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xx.sg.own.entity.CommunicationDevice;
import com.xx.sg.own.service.CommunicationDeviceService;
import com.xx.sg.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/communicationDevice")
@Tag(name = "通讯设备", description = "通讯设备API")
public class CommunicationDeviceController {

    @Autowired
    private CommunicationDeviceService communicationDeviceService;

    @PostMapping("/page")
    @Operation(summary = "分页通讯设备", description = "分页通讯设备")
    public Map page(@RequestParam(defaultValue = "1") Integer current,
                    @RequestParam(defaultValue = "10") Integer size,
                    @ModelAttribute CommunicationDevice device) {
        LambdaQueryWrapper<CommunicationDevice> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(device.getType()), CommunicationDevice::getType, device.getType())
                .eq(StringUtils.isNotBlank(device.getRegion()), CommunicationDevice::getRegion, device.getRegion());

        Page<CommunicationDevice> page = new Page<>(current, size);
        communicationDeviceService.page(page, wrapper);

        Map<String, Object> res = new HashMap<>();
        res.put("success", true);
        res.put("code", 200);
        res.put("message", "成功");
        res.put("data", page);
        return res;
    }

    @GetMapping("/info/{id}")
    @Operation(summary = "通讯设备详情", description = "通讯设备详情")
    public Map info(@PathVariable("id") String id) {
        CommunicationDevice device = communicationDeviceService.getById(id);
        Map<String, Object> res = new HashMap<>();
        res.put("success", true);
        res.put("code", 200);
        res.put("message", "成功");
        res.put("data", device);
        return res;
    }

    @PostMapping("/add")
    @Operation(summary = "添加通讯设备", description = "添加通讯设备")
    public Map add(CommunicationDevice device) {
        communicationDeviceService.save(device);
        Map<String, Object> res = new HashMap<>();
        res.put("success", true);
        res.put("code", 200);
        res.put("data", true);
        res.put("message", "成功");
        return res;
    }

    @PostMapping("/modify")
    @Operation(summary = "修改通讯设备", description = "修改通讯设备")
    public Map modify(CommunicationDevice device) {
        communicationDeviceService.updateById(device);
        Map<String, Object> res = new HashMap<>();
        res.put("success", true);
        res.put("code", 200);
        res.put("data", true);
        res.put("message", "成功");
        return res;
    }

    @GetMapping("/del/{id}")
    @Operation(summary = "删除通讯设备", description = "删除通讯设备")
    public Map del(@PathVariable String id) {
        communicationDeviceService.removeById(id);
        Map<String, Object> res = new HashMap<>();
        res.put("success", true);
        res.put("code", 200);
        res.put("data", true);
        res.put("message", "成功");
        return res;
    }

    @GetMapping("/checkStream")
    @Operation(summary = "流是否可用列表", description = "流是否可用列表")
    public R checkStream() {
        List<CommunicationDevice> list = communicationDeviceService.list();

//        for (CommunicationDevice communicationDevice : list) {
//            communicationDevice.setPlayStatus(streamPlayStatus(communicationDevice.getLinkAddress(),500));
//        }
        return R.success(list);
    }

    private boolean streamPlayStatus(String urlString, int timeOutMillSeconds) {
        int status = 404;
        try {

            URL urlObj = new URL(urlString);
            HttpURLConnection oc = (HttpURLConnection) urlObj.openConnection();
            oc.setUseCaches(false);
            oc.setConnectTimeout(timeOutMillSeconds); // 设置超时时间
            oc.setReadTimeout(timeOutMillSeconds);
            status = oc.getResponseCode();// 请求状态
            System.out.println(urlString + ":" + status);
        } catch (Exception e) {
            System.out.println(urlString + ":504");
        }
        return status == 200;
    }


}
